/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.eng.scc.core.parts;

import com.eng.scc.core.data.Company;
import com.eng.scc.core.data.Driver;
import com.eng.scc.core.db.manager.service.FuelControllerManagerService;
import com.eng.scc.core.util.ImageFinder;
import com.eng.scc.core.view.ApplicationContext;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

/**
 *
 * @author Rafael
 */
public class DriversTree extends JTree
{
    private static class DriverTreeRenderer 
                extends 
                    JPanel
                        implements 
                            TreeCellRenderer
    {
        private ImageIcon iconCompany = ImageFinder.getImageIcon( "tree_company.png" );
        private ImageIcon iconDriver = ImageFinder.getImageIcon( "tree_driver.png" );
        
        private Color defaultTextSelectionColor       = UIManager.getColor( "Tree.selectionForeground" );
        private Color defaultBackgroundSelectionColor = UIManager.getColor( "Tree.selectionBackground" );
        
        private JLabel labelIcon = new JLabel();
        private JLabel labelText = new JLabel();
        
        public DriverTreeRenderer()
        {
            setOpaque( false );
            
            setLayout( new GridBagLayout() );
            
            add( labelIcon, 
                 new GridBagConstraints( 0, 0, 1, 1, 0.0, 0.0, 
                                         GridBagConstraints.CENTER, 
                                         GridBagConstraints.NONE, 
                                         new Insets( 0, 0, 0, 0 ), 0, 0 ) );
            add( labelText, 
                 new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0, 
                                         GridBagConstraints.CENTER, 
                                         GridBagConstraints.NONE, 
                                         new Insets( 0, 5, 0, 0 ), 0, 0 ) );
            labelText.setOpaque( true );
        }
        
        @Override
        public Component getTreeCellRendererComponent( JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus )
        {
            labelText.setFont( tree.getFont() );
            labelText.setText( value.toString() );
            
            if ( ((DefaultMutableTreeNode) value).getUserObject() instanceof Company )
            {
                labelIcon.setIcon( iconCompany );
            }
            
            else if ( ((DefaultMutableTreeNode) value).getUserObject() instanceof Driver )
            {
                labelIcon.setIcon( iconDriver );
            }
            
            labelText.setBackground( tree.getBackground() );
            labelText.setForeground( tree.getForeground() );

            labelText.setBorder( BorderFactory.createEmptyBorder(5,5,5,5) );
            
            if ( selected )
            {
                labelText.setBackground( defaultBackgroundSelectionColor );
                labelText.setForeground( defaultTextSelectionColor );
                
                if ( hasFocus )
                {
                    labelText.setBorder( BorderFactory.createCompoundBorder( BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black), BorderFactory.createEmptyBorder(4,4,4,4) ) );
                }
                
                else
                {
                    labelText.setBorder( BorderFactory.createEmptyBorder(5,5,5,5) );
                }
            }
            
            return this;
        }
    }
    
    public DriversTree()
    {
        setCellRenderer( new DriverTreeRenderer() );
        setRootVisible( false );
        setShowsRootHandles( true );
        setRowHeight( 25 );
    }
    
    public void buildTree()
    {
        try
        {
            FuelControllerManagerService flm = FuelControllerManagerService.getInstance();
            
            List<Company> companies = flm.getCompanies();
            List<Driver> drivers    = flm.getDrivers();
            
            HashMap<Integer, DefaultMutableTreeNode> hashMap = new HashMap<Integer, DefaultMutableTreeNode>();
            
            if ( companies != null )
            {
                if ( drivers != null )
                {
                    for ( Driver p : drivers )
                    {
                        DefaultMutableTreeNode node = hashMap.get( p.getCompanyId() );
                        
                        if ( node == null )
                        {
                            hashMap.put( p.getCompanyId(), 
                                         node = new DefaultMutableTreeNode( flm.getCompany( p.getCompanyId() ) ) );
                        }
                        
                        node.add( new DefaultMutableTreeNode( p ) );
                    }
                }
                
                DefaultMutableTreeNode root = new DefaultMutableTreeNode();
                
                for ( DefaultMutableTreeNode treeNode : hashMap.values() )
                {
                    root.add( treeNode );
                }
                
                setModel( new DefaultTreeModel( root ) );
            }
            
            expandAll();
        }
        
        catch ( Exception e )
        {
            ApplicationContext.getInstance().saveLog( e );
        }
    }
    
    private void expandAll()
    {
        Enumeration e = ((DefaultMutableTreeNode)getModel().getRoot()).breadthFirstEnumeration();
        
        for(;e.hasMoreElements();)
        {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.nextElement();
            
            expandPath( new TreePath( node.getPath() ) );
        }
    }
}

